package edu.hawaii.duedates;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Uses JavaMail to send email to user with information on due items.
 * @author John Ly
 */
public class Mailer {
  
  /**
   * Sets up the fields for the email message and sends it.
   * @param toAddr The email of the recipient.
   * @param host The SMTP host.
   * @param bodyText The body of the text.
   * @return Returns true if successful.
   */
  public boolean send (String toAddr, String host, String bodyText) {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.from", "duedates-yellow-discuss@googlegroups.com");
    //props.put("mail.debug", "true");
    Session session = Session.getInstance(props, null);

    try {
        Message msg = new MimeMessage(session);
        msg.setFrom();
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddr));
        msg.setSubject("DueDates Alert");
        msg.setSentDate(new Date());
        msg.setText(bodyText);
        Transport.send(msg);
        return true;
    }
    catch (MessagingException mex) {
        System.out.println("send failed, exception: " + mex);
        return false;
    }    
  }
}

